package com.heo.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heo.dao.BaseMapper;
import com.heo.pojo.BaseEntity;
import com.heo.util.PageBean;

/**
 * 基础dao实现类
 * 
 * @author penggl
 * @version 1.0
 * @date 2017-5-29
 * @param <T>
 */
public abstract class BaseMapperImpl<T extends BaseEntity> implements BaseMapper<T>
{

  protected static Logger logger = LoggerFactory.getLogger(BaseMapperImpl.class);

  private static final String SQL_INSERT = "insert";
  private static final String SQL_BATCH_INSERT = "insertAll";
  private static final String SQL_UPDATE = "update";
  private static final String SQL_GET_BY_KEY = "getByKey";
  private static final String SQL_GET_BY_ENTITY = "getByEntity";
  private static final String SQL_GET_BY_MAP = "getByMap";

  private static final String SQL_DELETE = "delete";
  private static final String SQL_DELETE_BY_ID = "deleteById";

  private static final String SQL_LIST_ALL = "listAll";
  private static final String SQL_LIST_PAGE_BY_ENTITY = "listPageByEntity";
  private static final String SQL_LIST_PAGE_BY_MAP = "listPageByMap";
  private static final String SQL_LIST_BY_ENTITY = "listByEntity";
  private static final String SQL_LIST_BY_MAP = "listByMap";

  @Resource
  private SqlSession sqlSession;
  
  public SqlSession getSqlSession() {
		return sqlSession;
	}
  
  protected String getStatement(String sqlId)
  {
    String name = this.getClass().getName();
    return name + "." + sqlId;
  }

  public List<T> listAll()
  {
    try
    {
      List<T> list = sqlSession.selectList(getStatement(SQL_LIST_ALL));
      return list;
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public Page<T> listPageByEntity(PageBean pageBean, T entity)
  {
    try
    {
      return listPageByEntity(pageBean, entity, SQL_LIST_PAGE_BY_ENTITY);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public Page<T> listPageByEntity(PageBean pageBean, T entity, String sqlId)
  {
    try
    {
      if (pageBean != null)
      {
        PageHelper.startPage(pageBean.getStart(), pageBean.getLength());
      }
      List<T> list = sqlSession.selectList(getStatement(sqlId), entity);
      return (Page<T>) list;
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public Page<T> listPageByMap(PageBean pageBean, Map<String, Object> paramMap)
  {
    try
    {
      return listPageByMap(pageBean, paramMap, SQL_LIST_PAGE_BY_MAP);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public Page<T> listPageByMap(PageBean pageBean, Map<String, Object> paramMap, String sqlId)
  {
    try
    {
      if (pageBean != null)
      {
        PageHelper.startPage(pageBean.getStart(), pageBean.getLength());
      }
      List<T> list = sqlSession.selectList(getStatement(sqlId), paramMap);
      return (Page<T>) list;
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public T insert(T t) throws Exception
  {
    try
    {
      if (t == null)
      {
        logger.error("数据为null！");
        throw new Exception();
      }
      int result = sqlSession.insert(getStatement(SQL_INSERT), t);
      if (result <= 0)
      {
        throw new Exception();
      }
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
    return t;
  }

  @Override
  public long insertAll(List<T> list)
  {
    int result = 0;
    try
    {
      if (list == null || list.size() <= 0)
      {
        return 0;
      }

      result = sqlSession.insert(getStatement(SQL_BATCH_INSERT), list);

      if (result <= 0)
      {
        throw new Exception();
      }
      
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
    }
    return result;
  }

  @Override
  public long delete(T t)
  {
    int result = 0;
    try
    {
      result = sqlSession.delete(getStatement(SQL_DELETE), t);
      return result;
    }
    catch (Exception e)
    {
      throw e;
    }
  }

  @Override
  public long deleteById(long id)
  {
    try
    {
      return sqlSession.delete(getStatement(SQL_DELETE_BY_ID), id);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public long deleteByMap(Map<String, Object> paramMap, String sqlId)
  {
    try
    {
      return sqlSession.delete(getStatement(sqlId), paramMap);
    }
    catch (Exception e)
    {
      throw e;
    }
  }

  @Override
  public long update(T t)
  {
    int result = 0;
    try
    {
      if (t == null)
      {
        logger.error("数据为null！");
        throw new Exception();
      }
      result = sqlSession.update(getStatement(SQL_UPDATE), t);
      if (result <= 0)
      {
        throw new Exception();
      }
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
    }
    return result;
  }

  public long updateByMap(Map<String, Object> paramMap, String sqlId)
  {
    int result = 0;
    try
    {
      result = sqlSession.update(getStatement(sqlId), paramMap);
      if (result <= 0)
      {
        throw new Exception();
      }
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
    }
    return result;
  }

  @Override
  public T getByKey(Object key)
  {
    try
    {
      return sqlSession.selectOne(getStatement(SQL_GET_BY_KEY), key);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public T getByEntity(T t)
  {
    try
    {
      return getByEntity(t, SQL_GET_BY_ENTITY);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public T getByEntity(T t, String sqlId)
  {
    try
    {
      return sqlSession.selectOne(getStatement(sqlId), t);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public T getByMap(Map<String, Object> paramMap)
  {
    try
    {
      return getByMap(paramMap, SQL_GET_BY_MAP);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public T getByMap(Map<String, Object> paramMap, String sqlId)
  {
    try
    {
      return sqlSession.selectOne(getStatement(sqlId), paramMap);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public List<T> listByEntity(T t)
  {
    try
    {
      return listByEntity(t, SQL_LIST_BY_ENTITY);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public List<T> listByEntity(T t, String sqlId)
  {
    try
    {
      return sqlSession.selectList(getStatement(sqlId), t);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

  @Override
  public List<T> listByMap(Map<String, Object> paramMap)
  {
    try
    {
      return listByMap(paramMap, SQL_LIST_BY_MAP);
    }
    catch (Exception e)
    {
      throw e;
    }
  }

  @Override
  public List<T> listByMap(Map<String, Object> paramMap, String sqlId)
  {
    try
    {
      return sqlSession.selectList(getStatement(sqlId), paramMap);
    }
    catch (Exception e)
    {
      logger.error(e.getMessage(), e);
      throw e;
    }
  }

}
